<?php
class system_user_session extends system_session_namespace {
		
	public function __construct($namespance = 'USER') {
		parent::__construct($namespance);
	}
	/**
	 * @return system_user_session
	 */
	public function setId($id) {
		$this->uid = $id;
		return $this;
	}
	public function getId() {
		return $this->uid;
	}
	/**
	 * @return system_user_session
	 */
	public function setLanguage($lang) {
		$this->language = $lang;
		return $this;
	}
	public function getLanguage() {
		return $this->language ? $this->language : $this->_defaultLanguage();
	}
	/**
	 * @return system_user_session
	 */
	public function setIdentity($identity) {
		$this->identity = $identity;
		return $this;
	}
	public function getIdentity() {
		return $this->identity;
	}
	/**
	 * @return system_user_session
	 */
	public function setSecurity($secure) {
		$this->secure = ($secure ? md5(system_session::getId()) : NULL);
		return $this;
	}
	public function getSecurity() {
		return $this->secure;
	}

	/**
	 * @return system_user_session
	 */
	public function clean() {
		unset($this->uid);
		unset($this->language);
		unset($this->identity);
		unset($this->secure);
		return $this;
	}

	/**
	 * @return system_user_session
	 */
	public function logout() {
		unset($this->uid);
		unset($this->identity);
		unset($this->secure);
		return $this;
	}
	
	private function _defaultLanguage() {
		$lang = system_controller_front::getInstance()->getRequest()->getServer('HTTP_ACCEPT_LANGUAGE');
		if($lang && strlen($lang) >= 5) {
			$lang = substr($lang, 0, 5);
			$lang = str_replace('-', '_', $lang);
			return strtolower($lang);
		}
		return 'en_us';
	}
}
